﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Neat;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Neat.Mathematics;
using System.Diagnostics;
using Microsoft.Xna.Framework.Input;
using Neat.Graphics;
using Neat.GUI;
using Neat.Events;

namespace RiverRaidX
{
    public class NeatScreen : Screen
    {
        string font = "neatfont";

        Color background = new Color(75, 75, 75); // new Color(0, 153, 204);
        Color mainTextColor = Color.White; // new Color(0, 153, 204);
        float mainTextAlpha;

        float alpha = 0.0f;
        float black = 0.0f;

        EventManager eventManager;
        List<Event> Events;
        public NeatScreen(NeatGame game)
            : base(game)
        {
            Components.Add(eventManager = new EventManager(game));
            Events = eventManager.Events;
        }

        public override void LoadContent()
        {
            game.LoadFont(@"Fonts\neatfont");
            base.LoadContent();
        }

        public override void Activate()
        {
            game.Console.Run("e_framerate 60");
            base.Activate();
            Events.Clear();
            game.GameBackgroundColor = background;
            mainTextAlpha = 1;

            Events.Add(new TransitionTimerEvent(
                new TimeSpan(0, 0, 0),
                new TimeSpan(0, 0, 2),
                new TimeSpan(0, 0, 0, 0, 30),
                (o, e) => alpha += 0.03f));

            Events.Add(new TransitionTimerEvent(
                new TimeSpan(0, 0, 3),
                new TimeSpan(0, 0, 6),
                new TimeSpan(0, 0, 0, 0, 30),
                (o, e) => alpha -= 0.02f));

            Events.Add(new TimerOneTimeEvent(
                new TimeSpan(0, 0, 6),
                (o, e) => 
                    {//game.Console.Run("sh startscreen")
                        game.ActivateScreen("startscreen", new Neat.Transitions.Fader()
                        {
                            Length = new TimeSpan(0, 0, 0, 0, 500),
                            Rate = 2.0f
                        });
                    }));
        }

        public override void Behave(GameTime gameTime)
        {
            black = MathHelper.Clamp(black, 0, 1);
            base.Behave(gameTime);
        }

        public override void Render(GameTime gameTime)
        {
            var face = game.GetFont(font);
            var text = "This is Neat.";
            var size = face.MeasureString(text);
            var gameSize = new Vector2(game.GameWidth, game.GameHeight);

            game.SpriteBatch.DrawString(face, text, (gameSize - size) / 2.0f,
                GraphicsHelper.GetColorWithAlpha(mainTextColor, mainTextAlpha));

            game.SpriteBatch.Draw(game.GetTexture("solid"), new Rectangle(0, 0, game.GameWidth, game.GameHeight), new Color(new Vector4(new Vector3(0), 1.0f - alpha)));

            base.Render(gameTime);
        }
    }
}
